package com.ultrawise.survey.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.ultrawise.survey.core.base.Saveable;

@SuppressWarnings("all")
public class SQLUtils {

	public static Object[] generateInsert(Object obj, String tableName) {
		StringBuffer columnSB = new StringBuffer();
		StringBuffer paramSB = new StringBuffer();
		List params = new ArrayList();

		List fieldList = ReflectionUtils.findNotNullField(obj);
		try {
			for (int i = 0; i < fieldList.size(); i++) {
				Field field = (Field) fieldList.get(i);
				field.setAccessible(true);
				if (field.getName().equals("TABLENAME") || field.getName().equalsIgnoreCase("keyColumns")) {
					continue;
				}
				columnSB.append(field.getName());
				if (i != fieldList.size() - 3) {
					columnSB.append(",");
				}
				paramSB.append("?");
				if (i != fieldList.size() - 3) {
					paramSB.append(",");
				}
				params.add(field.get(obj));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		StringBuffer sb = new StringBuffer();
		sb.append("insert into ");
		sb.append(tableName);
		sb.append(" (");
		sb.append(columnSB);
		sb.append(") ");
		sb.append("values");
		sb.append(" (");
		sb.append(paramSB);
		sb.append(")");

		return new Object[] { sb.toString(), params.toArray() };
	}

	public static Object[] generateUpdate(Saveable obj, String tableName, String keyColumn) {
		StringBuffer columnSB = new StringBuffer();
		List params = new ArrayList();
		Object keyValue = null;

		List fieldList = ReflectionUtils.findAllField(obj);
		try {
			for (int i = 0; i < fieldList.size(); i++) {
				Field field = (Field) fieldList.get(i);
				field.setAccessible(true);
				if (field.getName().equals("TABLENAME") || field.getName().equalsIgnoreCase("keyColumns")) {
					continue;
				}
				if (field.getName().equalsIgnoreCase(keyColumn)) {
					keyValue = field.get(obj);
				} else {
					//判断当字段为空的时候就不做sql数据库的更新操作。
					if (field != null && getFieldValueByName(field.getName(), obj) != null) {
						columnSB.append(field.getName());
						columnSB.append("=?");
						if (i != fieldList.size() - 3) {
							columnSB.append(",");
						}
						params.add(field.get(obj));
					}
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		if (keyValue == null) {
			throw new IllegalArgumentException("key value for table '" + tableName + "' cannot be null.");
		} else {
			params.add(keyValue);
		}

		char c = columnSB.charAt(columnSB.length() - 1);

		if (String.valueOf(c).equalsIgnoreCase(",")) {
			columnSB.deleteCharAt(columnSB.length() - 1);
		}

		StringBuffer sb = new StringBuffer();
		sb.append("update ");
		sb.append(tableName);
		sb.append(" set ");
		sb.append(columnSB);
		sb.append(" ");
		sb.append("where ");
		sb.append(keyColumn);
		sb.append("=?");

		return new Object[] { sb.toString(), params.toArray() };
	}

	/***
	 * 根据反射机制获取对象的属性值信息
	 * 
	 * @param fieldName
	 * @param o
	 * @return
	 */
	private static Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter, new Class[] {});
			Object value = method.invoke(o, new Object[] {});
			return value;
		} catch (Exception e) {
			System.out.println("属性不存在");
			return null;
		}
	}
}
